<?php
defined('IN_RONCRM') or exit('No permission resources.');
class AdminManageController extends CAdmin {
	
	private $db,$role_db;
	
	public function __construct(){
		parent::__construct();	
		$this->db = new Admin_Model();
		$this->role_db = new Admin_role_Model();
	}
	
	
	public function init(){	
		include $this->admin_tpl('admin_list');
	}
	
	/*
	 * 管理员显示ajax,判断权限取到ajx_之后
	 */
	public function ajx_init(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : '1';
		$pagesize = 3;		
		$datas = $this->db->listinfo('',"username,roleid,lastloginip,lastlogintime,email,realname,userid", '', $page, $pagesize);
		$cache = new Caches();
		$role_cache = $cache->getcache('role', 'commons');
		$page_data = array();
		foreach ($datas as $data) {
			$data['role'] = $role_cache[$data['roleid']];
			$page_data[] = $data;
		}
		$head = "username,role,lastloginip,lastlogintime,email,realname";
		$page_head = explode(',',$head);
		$page_state = $this->db->pages; 		
		$page = array('page_data'=>$page_data,'page_head'=>$page_head,'page_state'=>$page_state);
		echo $infot = json_encode($page);
	}
		
	public function add(){
		if(isset($_POST['dosubmit'])){
			$info = array();
			if(!$this->checkname($_POST['info']['username'])){
				$this->showmessage('用户已存在'); 
			}
			$info = $this->checkuserinfo($_POST['info']);
			if(!$this->checkpasswd($info['password'])){
				$this->showmessage('密码不合法');
			}
			$passwordinfo = $this->password($info['password']);
			$info['password'] = $passwordinfo['password'];			
			$info['encrypt'] = $passwordinfo['encrypt'];
				
			$admin_fields = array('username', 'email', 'password', 'encrypt','roleid','realname');			
			foreach ($info as $k => $val){
				if(!in_array($k, $admin_fields)){
					unset($info[$k]);
				}
			}					
			$this->db->insert($info);
			if($this->db->insert_id()){
				$this->showmessage('操作成功！','?m=admin&c=adminmanage');
			}
		} else {
			$roles = $this->role_db->select();					
			include $this->admin_tpl('admin_add');
		}
		
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$info = $this->checkuserinfo($_POST['info']);
			if(isset($info['password']) && !empty($info['password'])){
				$this->edit_password($info['userid'], $info['password']);
			}
			$userid = $info['userid'];
			$admin_fields = array('username','email','roleid','realname');
			foreach($info as $k => $val){
				if(!in_array($k,$admin_fields)){
					unset($info[$k]);
				}
			}
			$this->db->update($info,array('userid' => $userid));
			$this->showmessage('操作成功！','?m=admin&c=adminmanage');
		} else {
			$info = $this->db->get_one(array('userid'=>$_GET['userid']));
			extract($info);	
			$roles = $this->role_db->select();		
			include $this->admin_tpl('admin_edit');		
		}
	}
	
	public function delete(){
		$userid = intval($_GET['userid']);
		$this->db->delete(array('userid' => $userid));
	}
	
	/*
	 * 检查用户名重名
	 */	
	private function checkname($username){
		$username = trim($username);
		if($this->db->get_one(array('username' => $username),'userid')){
			return false;
		}
		return true;
	}
	
	/*
	 * 验证数据
	 */
	private function checkuserinfo($data){
		$validator = new GlobalValidator();
		if(!is_array($data)){
			$this->showmessage('参数不完整'); return false;
		} elseif(!$validator->is_username($data['username'])){
			$this->showmessage('用户名不合法'); return false;
		} elseif(empty($data['email']) || !$validator->is_email($data['email'])){
			$this->showmessage('email不合法'); return false;
		} 
//		 elseif(empty($data['roleid'])){
//			return false;
//		} 
		return $data;
	}
	
	/**
	 * 对用户的密码进行加密
	 * @param $password
	 * @param $encrypt //传入加密串，在修改密码时做认证
	 * @return array/password
	 */
	private function password($password, $encrypt = ''){
		$process = new CProcess(); 
		$pwd = array();
		$pwd['encrypt'] = $encrypt ? $encrypt : $process->create_randonstr();
		$pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']);
		return  $encrypt ? $pwd['password'] : $pwd;
	}
	
	/*
	 * 检查密码
	 */
	private function checkpasswd($password){
		$validator = new GlobalValidator();
		if(!$validator->is_password($password)){
			return false;
		}
		return true;
	}
	
	/*
	 * 修改密码
	 */
	private function edit_password($userid, $password){
		$userid = intval($userid);
		if($userid < 1) return false;
		$validator = new GlobalValidator();
		if(!$validator->is_password($password)){
			$this->showmessage('密码不合法');
			return false;
		}
		$passwordinfo = $this->password($password);
		return $this->db->update($passwordinfo,array('userid'=>$userid));
	}
	
	/*
	 * 异步检测用户名
	 */
	public function public_checkname_ajx(){
		$username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit(0);
		if ($this->db->get_one(array('username'=>$username),'userid')){
			exit('0');
		} 
		exit('1');		
	}

}